import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useMemberCardStore = defineStore('memberCard',() => {
    // 模拟会员卡数据
    const memberCards = ref([
        {
            id: 1,
            memberCardName: "金卡",
            price: 450,
            description: '',
            remark: '',
            type: "次卡(无期限)",
            availableTimes: 3,
            availableDays: 365,
            supportCourses: [2],
            num: 1
        },
        {
            id: 2,
            memberCardName: "银卡",
            price: 300,
            description: '',
            remark: '',
            type: "次卡(无期限)",
            availableTimes: 2,
            availableDays: 60,
            supportCourses: [1],
            num: 0
        },
    ])

    const getMemberCardById = (id) => {
        return memberCards.value.find(item => item.id === id)
    }

    const getMemberCardIdByName = (name) => {
        return memberCards.value.find(item => item.memberCardName === name).id
    }

    const addMemberCard = (memberCard) => {
        memberCards.value.push({
            id: memberCards.value.length + 1,
            memberCardName: memberCard.memberCardName,
            description: memberCard.description,
            remark: memberCard.remark,
            price : memberCard.price,
            type: memberCard.type,
            availableTimes: memberCard.availableTimes,
            availableDays: memberCard.availableDays,
            supportCourses: memberCard.supportCourses,
        });
    }

    const editMemberCard = (memberCard) => {
        const temp = memberCards.value.find(item => item.id === memberCard.id)
        temp.memberCardName = memberCard.memberCardName     
        temp.description = memberCard.description
        temp.remark = memberCard.remark
        temp.price = memberCard.price
        temp.type = memberCard.type       
        temp.availableTimes = memberCard.availableTimes
        temp.availableDays = memberCard.availableDays
        temp.supportCourses = memberCard.supportCourses
    }

    const deleteMemberCard = (id) => {
        const index = memberCards.value.findIndex(item => item.id === id)
        memberCards.value.splice(index, 1)
    }

    const getMemberCardNum = () =>{
        const res = []
        memberCards.value.forEach(memeberCard =>{
            res.push({
                name: memeberCard.memberCardName,
                value: memeberCard.num
            })
        })
        return res
    }

    return {memberCards,
        getMemberCardById,
        getMemberCardIdByName,
        getMemberCardNum,
        addMemberCard,
        editMemberCard,
        deleteMemberCard
    }
})
